package com.byd.android.mediaplayer;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.provider.MediaStore;

import androidx.annotation.Nullable;

public abstract class BaseActivity extends Activity {

    private Intent mServiceIntent;

    protected MediaService.MediaBinder mServiceBinder;

    protected abstract void sendHandlerToService();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mServiceIntent = new Intent(this, MediaService.class);
    }

    private void setServiceBinder(IBinder iBinder) {
        mServiceBinder = (MediaService.MediaBinder) iBinder;
        sendHandlerToService();
    }

    protected void destroyServiceBinder() {
        if (mServiceBinder != null) {
            mServiceBinder.distoryHandler();
            mServiceBinder = null;
        }
    }


    ServiceConnection mServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            setServiceBinder(iBinder);
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            destroyServiceBinder();
        }
    };


    @Override
    protected void onResume() {
        super.onResume();
        bindService(mServiceIntent, mServiceConnection, BIND_AUTO_CREATE);

    }

    @Override
    protected void onPause() {
        super.onPause();
        unbindService(mServiceConnection);

    }
}
